﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GamePlugin.Logics.Core;
using WindowsInput;
using GamePlugin.GGraphics;
using GamePlugin.IO;

namespace GamePlugin.Logics.Game.Actions
{
    public class DActionRun : MachineState
    {
        public override void Initialize()
        {
            this.AddState(typeof(DActionRunItemSearchStart).Name, new DActionRunItemSearchStart());
            this[typeof(DActionRunItemSearchStart).Name].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.CheckKeys(o, e));

            this.AddState(typeof(DActionRunInputItemName).Name, new DActionRunInputItemName());
            this[typeof(DActionRunInputItemName).Name].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.CheckKeys(o, e));

            this.AddState(typeof(DActionRunInputBuyPrice).Name, new DActionRunInputBuyPrice());
            this[typeof(DActionRunInputBuyPrice).Name].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.CheckKeys(o, e));

            this.AddState(typeof(DActionRunSelectOccupation).Name, new DActionRunSelectOccupation());
            this[typeof(DActionRunSelectOccupation).Name].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.CheckKeys(o, e));

            this.AddState(typeof(DActionRunSelectEquip).Name, new DActionRunSelectEquip());
            this[typeof(DActionRunSelectEquip).Name].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.CheckKeys(o, e));

            this.AddState(typeof(DActionRunSelectItem).Name, new DActionRunSelectItem());
            this[typeof(DActionRunSelectItem).Name].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.CheckKeys(o, e));

            this.AddState(typeof(DActionRunSelectItemRarity).Name, new DActionRunSelectItemRarity());
            this[typeof(DActionRunSelectItemRarity).Name].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.CheckKeys(o, e));

            this.AddState(typeof(DActionRunSearchBuyAution).Name, new DActionRunSearchBuyAution());
            this[typeof(DActionRunSearchBuyAution).Name].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.CheckKeys(o, e));

            for (int propertyIndex = 0; propertyIndex < ActionData.PropertyCount; propertyIndex++)
            {
                DActionRunProperty.HelperPropertyIndex = propertyIndex;
                this.AddState(Helper.GetPropertyStateName(propertyIndex), new DActionRunProperty(propertyIndex));
                this[Helper.GetPropertyStateName(propertyIndex)].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.CheckKeys(o, e));
            }

            this.AddState(typeof(DActionRunItemSearchEnd).Name, new DActionRunItemSearchEnd());
            this[typeof(DActionRunItemSearchEnd).Name].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.CheckKeys(o, e));

            //this.DefaultState = Helper.GetPropertyStateName(0);
            this.DefaultState = typeof(DActionRunItemSearchStart).Name;
        }

        public void CheckKeys(object o, GameTimeEventArgs e)
        {
            InputSimulator input = Helper.DiabloPluginGame.InputSimulator;

            // stop
            if (input.InputDeviceState.IsKeyDown(WindowsInput.Native.VirtualKeyCode.F4))
            {
                Helper.Log("End of Diablo Plugin");
                IOManager.Instance.CloseLogFile();
                this.Owner.ChangeToState(typeof(DActionIdle).Name);
                this.IsNeedReset = true;
                this.Owner.IsNeedReset = true;
            }
        }
    }
}
